package com.github.plugin;

import android.content.Intent;
import android.text.TextUtils;

import com.github.livebase.log.LogUtil;
import com.github.livebase.log.LoggerFactory;
import com.github.livebase.util.LiveContext;
import com.github.livebase.util.base.ObjectsUtil;
import com.github.server.core.SimpleHandler;
import com.github.server.interfaces.HttpHandler;
import com.github.server.interfaces.HttpRequest;
import com.github.server.interfaces.HttpResponse;

import java.io.IOException;

/**
 * @ClassName ControllerH
 * @description:
 * @author: biao532
 * @create: 2024-02-01 13:19
 * @Version 1.0
 **/
public class HttpRemoteControl extends SimpleHandler implements HttpHandler {
    private LiveContext context;
    public HttpRemoteControl(String path, LiveContext context) {
        super(path);
        this.context = context;
    }
    @Override
    public void handler(HttpRequest request, HttpResponse response) {
        String number = request.getParameter("number");
        if (TextUtils.isEmpty(number)) {
            response.setCode(403);
            return;
        }
        try {
            try {
                Intent action = new Intent("com.github.live.CHANNEL_OPEN");
                action.putExtra("number", Integer.valueOf(number));
                context.sendBroadcast(action);
                response.write("OK");
            } catch (NumberFormatException e) {
                response.write("please check your input");
            }
        }
        catch (IOException e) {
            response.setCode(403);
            LoggerFactory.getLogger(HttpRemoteControl.class).warn("HttpRemoteControl", e
                    , LogUtil.ERROR_OCCURRED, "remoteControl");
        }
    }
}
